import axios from "axios";
//引入进度条
import nprogress from "nprogress";
//引入进度条样式
import "nprogress/nprogress.css";
//引入store
import store from "@/store";
const request = axios.create({
  //基础路径
  baseURL: "http://39.98.123.211",
  //请求超时的时间
  timeout: 5000,
});
//请求拦截器
request.interceptors.request.use((config) => {
  if (store.state.detail.uuid) {
    //给请求头添加一个字段,userTempId为后台的临时字段
    config.headers.userTempId = store.state.detail.uuid;
  }
  //携带token到服务器
  let token = localStorage.getItem("token");
  if (token) {
    config.headers.token = token;
  }
  //进度条开始
  nprogress.start();
  return config;
});
//响应拦截器
request.interceptors.response.use(
  (res) => {
    //成功
    //进度条结束
    nprogress.done();
    return res.data;
  },
  (err) => {
    //失败
    return Promise.reject(err);
  }
);
export default request;
